package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author
 * @date 2020/12/5 20:39
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    CollectMapper collectMapper;
    @Autowired
    FootprintMapper footprintMapper;
    @Autowired
    SearchHistoryMapper searchHistoryMapper;
    @Autowired
    FeedbackMapper feedbackMapper;


    @Override
    public ListData<User> queryList(Integer page, Integer limit, String sort, String order, String username, String mobile) {
        PageHelper.startPage(page, limit);

        UserExample userExample = new UserExample();
        userExample.setOrderByClause(sort + " " + order);
        UserExample.Criteria criteria = userExample.createCriteria();
        if (username != null) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (mobile != null) {
            criteria.andMobileLike("%" + mobile + "%");
        }
        List<User> users = userMapper.selectByExample(userExample);
        ListData<User> listData = new ListData<>();
        listData.setItems(users);

        PageInfo<User> userPageInfo = new PageInfo<>(users);
        long total = userPageInfo.getTotal();
        listData.setTotal(total);
        return listData;
    }

    @Override
    public ListData<Address> queryAddressList(Integer page, Integer limit, String sort, String order) {
        PageHelper.startPage(page, limit);
        AddressExample addressExample = new AddressExample();
        addressExample.setOrderByClause(sort + " " + order);

        List<Address> addresses = addressMapper.selectByExample(addressExample);
        //todo 添加地址详细信息   AddressListVO类

        ListData<Address> listData = new ListData<>();
        listData.setItems(addresses);

        PageInfo<Address> addressPageInfo = new PageInfo<>(addresses);
        long total = addressPageInfo.getTotal();
        listData.setTotal(total);
        return listData;
    }

    @Override
    public ListData<Collect> queryCollectList(Integer page, Integer limit, String sort, String order, Integer userId, Integer valueId) {
        PageHelper.startPage(page, limit);
        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null) {
            criteria.andValueIdEqualTo(valueId);
        }
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        ListData<Collect> listData = new ListData<>();
        listData.setItems(collects);

        PageInfo<Collect> collectPageInfo = new PageInfo<>(collects);
        long total = collectPageInfo.getTotal();
        listData.setTotal(total);
        return listData;
    }

    @Override
    public ListData<Footprint> queryFootprintList(Integer page, Integer limit, String sort, String order, Integer userId, Integer goodsId) {
        PageHelper.startPage(page, limit);
        FootprintExample footprintExample = new FootprintExample();
        FootprintExample.Criteria criteria = footprintExample.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (goodsId != null) {
            criteria.andGoodsIdEqualTo(goodsId);
        }
        List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);
        ListData<Footprint> listData = new ListData<>();
        listData.setItems(footprints);

        PageInfo<Footprint> footprintPageInfo = new PageInfo<>(footprints);
        long total = footprintPageInfo.getTotal();
        listData.setTotal(total);
        return listData;
    }

    @Override
    public ListData<SearchHistory> queryHistoryList(Integer page, Integer limit, String sort, String order, Integer userId, String keyword) {
        PageHelper.startPage(page, limit);
        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (keyword != null) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);
        ListData<SearchHistory> listData = new ListData<>();
        listData.setItems(searchHistories);

        PageInfo<SearchHistory> searchHistoryPageInfo = new PageInfo<>(searchHistories);
        long total = searchHistoryPageInfo.getTotal();
        listData.setTotal(total);
        return listData;
    }

    @Override
    public ListData<Feedback> queryFeedbackList(Integer page, Integer limit, String sort, String order, Integer id, String username) {
        PageHelper.startPage(page, limit);
        FeedbackExample feedbackExample = new FeedbackExample();
        FeedbackExample.Criteria criteria = feedbackExample.createCriteria();
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        if (username != null) {
            criteria.andUsernameLike("%" + username + "%");
        }
        List<Feedback> feedbacks = feedbackMapper.selectByExample(feedbackExample);
        ListData<Feedback> listData = new ListData<>();
        listData.setItems(feedbacks);

        PageInfo<Feedback> feedbackPageInfo = new PageInfo<>(feedbacks);
        long total = feedbackPageInfo.getTotal();
        listData.setTotal(total);
        return listData;
    }

    @Override
    public List<User> selectUserByUsername(String username) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<User> users = userMapper.selectByExample(userExample);
        return users;
    }

    @Override
    public List<String> selectPermissionByUserId(Integer userId) {
        List<String> permissions=userMapper.selectPermissionByUserId(userId);
        return permissions;
    }

    @Override
    public UserInfo selectUserInfo(Integer userId) {
        UserInfo userInfo =userMapper.selectUserInfo(userId);
        return userInfo;
    }
}
